package com.cts.tmall.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cts.tmall.com.exception.SystemException;
import com.cts.tmall.com.message.RspCode;
import com.cts.tmall.dal.entity.TmallCoupon;
import com.cts.tmall.dal.mapper.TmallCouponMapper;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.SimpleDateFormat;
import java.util.Map;

@Slf4j
@Service
public class CouponService {

    @Autowired
    private TmallCouponMapper couponMapper;

    public Map<String, Object> queryCoupon(Long createUserId,String couponNo, String couponName, String couponType,
                                           String useStartTime, String useEndTime, String status,
                                           String couponChannel, Long pageNumb, Long pageSize) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LambdaQueryWrapper<TmallCoupon> wrapper = Wrappers.<TmallCoupon>lambdaQuery()
                .eq(TmallCoupon::getCreateUserId,createUserId)
                .eq(StringUtils.isNotBlank(couponNo), TmallCoupon::getCouponNo, StringUtils.isNotBlank(couponNo) ? Long.parseLong(couponNo) : null)
                .eq(StringUtils.isNotBlank(couponName), TmallCoupon::getCouponName, couponName)
                .eq(StringUtils.isNotBlank(couponType), TmallCoupon::getCouponType, couponType)
                .ge(StringUtils.isNotBlank(useStartTime), TmallCoupon::getUseEndTime, StringUtils.isNotBlank(useStartTime) ? sdf.parse(useStartTime) : null)
                .le(StringUtils.isNotBlank(useEndTime), TmallCoupon::getUseStartTime, StringUtils.isNotBlank(useEndTime) ? sdf.parse(useEndTime) : null)
                .eq(StringUtils.isNotBlank(status), TmallCoupon::getStatus, status)
                .eq(StringUtils.isNotBlank(couponChannel), TmallCoupon::getCouponChannel, couponChannel);

        Page<TmallCoupon> page = new Page<>(pageNumb, pageSize);
        page = couponMapper.selectPage(page, wrapper);
        return ImmutableMap.<String, Object>builder()
                .put("pageNumb", page.getCurrent())
                .put("pageSize", page.getSize())
                .put("total", page.getTotal())
                .put("data", page.getRecords())
                .build();
    }

    public TmallCoupon addCoupon(TmallCoupon coupon) {
        if (StringUtils.isBlank(coupon.getCouponChannel())) {
            log.error("推广渠道不能为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "推广渠道为空！");
        }
        if (StringUtils.isBlank(coupon.getCouponName())) {
            log.error("优惠券名称不能为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "优惠券名称为空！");
        }
        if (StringUtils.isBlank(coupon.getCouponType())) {
            log.error("优惠券类型不能为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "优惠券类型为空！");
        }
        if (StringUtils.isBlank(coupon.getCouponDiscount())) {
            log.error("折扣不能为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "折扣为空！");
        }
        if (StringUtils.isBlank(coupon.getCouponCount())) {
            log.error("发放量不能为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "发放量为空！");
        }
        if (StringUtils.isBlank(coupon.getLimitCount())) {
            log.error("每人限领不能为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "每人限领为空！");
        }
        if (StringUtils.isBlank(coupon.getCouponRedirect())) {
            log.error("领券后跳转不能为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "领券后跳转为空！");
        }
        coupon.setGetCount("0");
        coupon.setUseDateCount("0");
        coupon.setStatus("01");
        coupon.setUsedCount("0");
        couponMapper.insert(coupon);
        return couponMapper.selectById(coupon.getCouponNo());
    }

    public TmallCoupon updateCoupon(TmallCoupon coupon) {
        if (coupon.getCouponNo() == null) {
            log.error("优惠券批次号不能为空！");
            throw new SystemException(RspCode.FAILURE_PARAMS, "优惠券批次号为空！");
        }
        couponMapper.updateById(coupon);
        return couponMapper.selectById(coupon.getCouponNo());
    }
}
